
/* Generated by Interface Builder */

#import "Reson.h"
#import "Instrum.h"
#import <appkit/graphics.h>
#import "Statement.h"

@implementation Reson

+ initialize
{
   	image = [NXImage findImageNamed:"Reson"];
	cfOffset.x = 3;
	cfOffset.y = 30;
	inOffset.x = 30;
	inOffset.y = 51;
	bwOffset.x = 52;
	bwOffset.y = 30;
	outOffset.x = 29;
	outOffset.y = 3;
	
	return self;
}

- init
{
	written = NO;
	size.width = 80.;
	size.height = 80.;
	center.x = size.width/2.;
	center.y = size.height/2.;
	strcpy(type,"Reson");
	strcpy(name,"RESON");	// cmix macro
	
	cf = [[Param alloc] init:self :&cfOffset];
	in = [[Param alloc] init:self :&inOffset];
	bw = [[Param alloc] init:self :&bwOffset];
	out = [[Param alloc] init:self :&outOffset];

	[cf setValue:"440"];	// set non-"1" default values
	[bw setValue:"20"];	
	[cf setTitle:"CFreq:"];
	[bw setTitle:"BWid"];
	[in setTitle:"Input:"];
	[out setTitle:"Out:"];
		
	paramList = [[List alloc] initCount:5];
	[paramList addObject:cf];
	[paramList addObject:in];
	[paramList addObject:bw];
	[paramList addObject:out];
	
	[Inst putUgenInList:self];
	
	return self;
}

- remove
{
	id cp,param;
	int i;
							// make sure not connected first 
	for(i = 0; i < ([paramList count]); i++) {	
		param = [paramList objectAt:i];
		if(cp = [param getConnectedParam]) 
			return 0;
	}
							// remove ugen from list
	[Inst removeUgenFromList:self];
	return self;
}

- (NXImage *)getImage
{
	return image;
}

- move:(NXPoint *)newloc
{
	location = *newloc;
	[cf move:&location];
	[in move:&location];
	[bw move:&location];
	[out move:&location];
	return self;
}

- findParamAtPoint:(NXPoint *)point
{
	NXRect *rect;
	int i;
	id param;
	
	for(i = 0; i < [paramList count]; i++) {
		param = [paramList objectAt:i];
		rect = [param getRect];
		if(NXMouseInRect(point,rect,NO))
			return param;
	}
	//printf("no param found\n");
	return nil;
}

- writeUgen
{
	BOOL ai = NO;	// amplitude input ugen
	BOOL ri = NO;	// rise input ugen
	BOOL di = NO;	// decay input ugen
	char ampVar[10];
	char par[20];
	/* 	for amp, rise, & decay input params:
			see if there is input ugen
				if so, grab the output (ug?) and multiply it by this
					param value
				if not just use the param value
	 	after all params done, write relevant code into lists
			(declarations, assignments and loop statements)
			i.e. ug? = oscil(,,,,);
		go to output param connected ugen and call its "writeUgen"
	*/
	int i;
	int parent[4];
	id param;
	id ug;
	id nupar;
	char str[50];
	
	//printf("writing ugen %d\n",index);
	sprintf(str,"\tfloat ug%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat amp%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat cf%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat in%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat bw%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat scl%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat xinit%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat val%d;\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat rsnetc%d[5];\n",index);
	[Inst putVarInList:str];
	sprintf(str,"\tfloat out%d;\n",index);
	[Inst putVarInList:str];

	for(i = 0; i < ([paramList count]-1); i++) {	
		param = [paramList objectAt:i];
		if(nupar = [param getConnectedParam]) {
			ug = [nupar getUgen];
			if([ug getWritten] == NO) { 
				parent[i] = [[ug writeUgen] getIndex];
			}
			else  {
				parent[i] = 0;
			}
			//printf("parent[%d] = %d\n",i,parent[i]);
		}
		else 
			parent[i] = 0;
		
	}
	
	strcpy(par,[[paramList objectAt:0] getValue]);
	if(par[0] == 'p' && par[1] == '[')
		sprintf(str,"\tcf%d = %s;\n", index, par);
	else
		sprintf(str,"\tcf%d = %.2f;\n", index, atof(par));
	[Inst putAssignInList:str];
	
	strcpy(par,[[paramList objectAt:1] getValue]);
	if(par[0] == 'p' && par[1] == '[')
		sprintf(str,"\tamp%d = %s;\n", index, par);
	else
		sprintf(str,"\tamp%d = %.2f;\n", index, atof(par));
	[Inst putAssignInList:str];
	
	strcpy(par,[[paramList objectAt:2] getValue]);
	if(par[0] == 'p' && par[1] == '[')
		sprintf(str,"\tbw%d = %s;\n", index, par);
	else
		sprintf(str,"\tbw%d = %.2f;\n", index, atof(par));
	[Inst putAssignInList:str];
	

	if(parent[1]) { 	//  input ugen	
		//printf("input ugen to reson\n");
		ai = YES;
		sprintf(str,"\tin%d = %.2f;\n",index, 
			atof([[paramList objectAt:1] getValue]));
		[Inst putAssignInList:str];
		sprintf(str,"\tfloat inai%d;\n",index);
		[Inst putVarInList:str];
		sprintf(str,"\t\tinai%d = amp%d * ug%d;\n",index,  
			index,parent[1]);
		[Inst putLoopInList:str];
	}
	else {				// input preset or default
		//printf("input preset or default\n");
 		strcpy(par,[[paramList objectAt:0] getValue]);
		if(par[0] == 'p' && par[1] == '[')
			sprintf(str,"\tamp%d = %s;\n", index, par);
		else
			sprintf(str,"\tamp%d = %.2f;\n",index,atof(par));
		[Inst putAssignInList:str];
	}
	if(ai)
		sprintf(ampVar,"inai%d",index);
	else
		sprintf(ampVar,"amp%d",index);
		
	if(!strcmp([[[in getConnectedParam] getUgen] getType],"Rand")) 
		sprintf(str,"\tscl%d = 2;\n", index, par);
	else
		sprintf(str,"\tscl%d = 1;\n", index, par);
	[Inst putAssignInList:str];
	sprintf(str,"\txinit%d = 0;\n", index, par);
	[Inst putAssignInList:str];
	sprintf(str,"\trsnetc%d[3] = rsnetc%d[4] = 0;\n", index, index);
	[Inst putAssignInList:str];
		 
						// 	set up reson filter
	sprintf(str, "\trsnset(cf%d,bw%d,scl%d,xinit%d,rsnetc%d);\n",index,index,index, index,index);
	[Inst putAssignInList:str];
	
					// output amplitude multiplier:
	strcpy(par,[[paramList objectAt:3] getValue]);
	if(par[0] == 'p' && par[1] == '[')
		sprintf(str,"\tout%d = %s;\n",index,par);
	else
		sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:3] getValue]));
	[Inst putAssignInList:str];

	sprintf(str,"\t\t%s(val%d,%s,rsnetc%d);\n", name, index, ampVar, index);
	[Inst putLoopInList:str];
	sprintf(str,"\t\tug%d = val%d*out%d;\n", index, index, index);
	[Inst putLoopInList:str];
	
	written = YES;
	return self;	
}


@end
